import scrapy
from ..items import DoubanItem
import re


class DoubanmSpider(scrapy.Spider):
    name = 'doubanm'
    allowed_domains = ['douban.com']
    start_urls = ['https://movie.douban.com/top250']

    def start_requests(self):
        url = 'https://accounts.douban.com/j/mobile/login/basic'
        data = {
            'remember': 'true',
            'name': '15521184094',
            'password': '123456'
        }

        yield scrapy.FormRequest(url=url, formdata=data, callback=self.pase1)

    def pase1(self, response):
        print(response.body)
        print('----------->')

        if '15521184094' in response.body.decode():
            print("登录成功")
            # 继续写爬虫逻辑
            yield scrapy.Request(url=self.start_urls[0], callback=self.parse)

        else:
            print("登录失败")
            # top250不管是否登录成功都可以继续写爬虫逻辑
            yield scrapy.Request(url=self.start_urls[0], callback=self.parse)

    def parse(self, response):
        movie_list = response.xpath('//div[@class="info"]')

        for movie in movie_list:
            item = DoubanItem()

            item["title"] = (movie.xpath(".//span[@class='title']/text()").extract_first())
            # item["movieinfo"] = movie.xpath(".//div[@class='bd']/p/text()").extract_first()
            st = movie.xpath(".//div[@class='bd']/p/text()").extract_first()
            item["movieinfo"] = st.strip()
            item["star"] = movie.xpath(".//span[@class='rating_num']/text()").extract_first()
            item["quote"] = movie.xpath(".//span[@class='inq']/text()").extract_first()

            yield item

            # 写下一页。。。。
